import { ecs } from "../../core/ecs/ECS";
import { CommonFactory } from "../../module/common/CommonFactory";
import { GateModel } from "./model/GateModel";
import { GateServiceStartComp, GateServiceStartSystem } from "./sys/GateServiceStartSys";

@ecs.register(`GateService`)
export class GateService extends ecs.Entity {
    GateModel!: GateModel;

    protected init() {
        this.addComponents<ecs.Comp>(
            GateModel);
    }

    /** 开启网关服务器 */
    start() {
        this.add(GateServiceStartComp);
    }

    /**账号注册到大厅服务器 */
    async enterHall(addr:string, sso?:string) {
        let hc = null;
        if (this.GateModel.hallDict.has(addr)) {
            hc = this.GateModel.hallDict.get(addr)
            
        } else {
            hc = CommonFactory.createHcHall();
            this.GateModel.hallDict.set(addr, hc);
        }
        if (hc) {
            const ret = await hc.callApi(`Join`, {
                sso: sso||""
            })
            return ret.isSucc
        } else {
            return false;
        }
    }
}

export class EcsGateSystem extends ecs.System {
    constructor() {
        super();

        this.add(new GateServiceStartSystem());
    }
}

/** 网关服务器模块 */
export class GateServiceRootSystem extends ecs.RootSystem {
    constructor() {
        super();
        this.add(new EcsGateSystem());
    }
}